﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EARS.App_Code;

namespace EARS
{
    public partial class SearchResult : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                string SearchText = Session["SearchText"].ToString();
                Event evt = new Event();
                List<string> eventList = evt.Search(SearchText);
                if (eventList.Count != 0)
                {
                    pnlNoResult.Visible = false;
                    if (eventList.Count < 10)
                    {
                        btnView.Visible = false;
                        for (int i = 1; i <= eventList.Count; i++)
                        {
                            Event eventDetails = evt.GetEvent(eventList[i - 1]);
                            Fill(i, eventDetails);
                        }
                        Session.Abandon();
                        int num = eventList.Count + 1;
                        for (int j = num; j <= 10; j++)
                        {
                            Hide(j);
                        }
                    }
                    else
                    {
                        btnView.Visible = true;
                        for (int i = 1; i <= 10; i++)
                        {
                            Event eventDetails = evt.GetEvent(eventList[i - 1]);
                            Fill(i, eventDetails);
                        }
                    }
                }
                else
                {
                    for (int j = 1; j <= 10; j++)
                    {
                        Hide(j);
                    }
                    pnlNoResult.Visible = true;
                }
            }
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            string SearchText = Session["SearchText"].ToString();
            Event evt = new Event();
            Label EventName = (Label)EventNotification10.FindControl("lblEventName");
            string dateCreated = evt.GetDateCreated(EventName.Text);
            List<string> eventList = evt.Search(SearchText, dateCreated);
            if (eventList.Count != 0)
            {
                pnlNoResult.Visible = false;
                if (eventList.Count < 10)
                {
                    btnView.Visible = false;
                    for (int i = 1; i <= eventList.Count; i++)
                    {
                        Event eventDetails = evt.GetEvent(eventList[i - 1]);
                        Fill(i, eventDetails);
                    }
                    Session.Abandon();
                    int num = eventList.Count + 1;
                    for (int j = num; j <= 10; j++)
                    {
                        Hide(j);
                    }
                }
                else
                {
                    btnView.Visible = true;
                    for (int i = 1; i <= 10; i++)
                    {
                        Event eventDetails = evt.GetEvent(eventList[i - 1]);
                        Fill(i, eventDetails);
                    }
                }
            }
            else
            {
                for (int j = 1; j <= 10; j++)
                {
                    Hide(j);
                }
                pnlNoResult.Visible = true;
            }
        }

        public void Fill(int i, Event e)
        {
            Participation p = new Participation();
            Label lblEventName = null;
            Label lblVenue = null;
            Label lblStartDate = null;
            Label lblEndDate = null;
            Label lblStartTime = null;
            Label lblEndTime = null;
            Label lblDeadline = null;
            Label lblDescription = null;
            Label lblNumber = null;
            switch (i)
            {
                case 1:
                    lblEventName = (Label)EventNotification1.FindControl("lblEventName");
                    lblEventName.Text = e.EventName;
                    lblVenue = (Label)EventNotification1.FindControl("lblVenue");
                    lblVenue.Text = e.EventVenue;
                    lblStartDate = (Label)EventNotification1.FindControl("lblStartDate");
                    lblStartDate.Text = e.StartDate.ToShortDateString();
                    lblEndDate = (Label)EventNotification1.FindControl("lblEndDate");
                    lblEndDate.Text = e.EndDate.ToShortDateString();
                    lblStartTime = (Label)EventNotification1.FindControl("lblStartTime");
                    lblStartTime.Text = e.StartTime;
                    lblEndTime = (Label)EventNotification1.FindControl("lblEndTime");
                    lblEndTime.Text = e.EndTime;
                    lblDeadline = (Label)EventNotification1.FindControl("lblDeadline");
                    lblDeadline.Text = e.EventDeadline.ToShortDateString();
                    lblDescription = (Label)EventNotification1.FindControl("lblDescription");
                    lblDescription.Text = e.Description;
                    lblNumber = (Label)EventNotification1.FindControl("lblNumber");
                    lblNumber.Text = p.NumberOfParticipant(lblEventName.Text).ToString();
                    break;
                case 2:
                    lblEventName = (Label)EventNotification2.FindControl("lblEventName");
                    lblEventName.Text = e.EventName;
                    lblVenue = (Label)EventNotification2.FindControl("lblVenue");
                    lblVenue.Text = e.EventVenue;
                    lblStartDate = (Label)EventNotification2.FindControl("lblStartDate");
                    lblStartDate.Text = e.StartDate.ToShortDateString();
                    lblEndDate = (Label)EventNotification2.FindControl("lblEndDate");
                    lblEndDate.Text = e.EndDate.ToShortDateString();
                    lblStartTime = (Label)EventNotification2.FindControl("lblStartTime");
                    lblStartTime.Text = e.StartTime;
                    lblEndTime = (Label)EventNotification2.FindControl("lblEndTime");
                    lblEndTime.Text = e.EndTime;
                    lblDeadline = (Label)EventNotification2.FindControl("lblDeadline");
                    lblDeadline.Text = e.EventDeadline.ToShortDateString();
                    lblDescription = (Label)EventNotification2.FindControl("lblDescription");
                    lblDescription.Text = e.Description;
                    lblNumber = (Label)EventNotification2.FindControl("lblNumber");
                    lblNumber.Text = p.NumberOfParticipant(lblEventName.Text).ToString();
                    break;
                case 3:
                    lblEventName = (Label)EventNotification3.FindControl("lblEventName");
                    lblEventName.Text = e.EventName;
                    lblVenue = (Label)EventNotification3.FindControl("lblVenue");
                    lblVenue.Text = e.EventVenue;
                    lblStartDate = (Label)EventNotification3.FindControl("lblStartDate");
                    lblStartDate.Text = e.StartDate.ToShortDateString();
                    lblEndDate = (Label)EventNotification3.FindControl("lblEndDate");
                    lblEndDate.Text = e.EndDate.ToShortDateString();
                    lblStartTime = (Label)EventNotification3.FindControl("lblStartTime");
                    lblStartTime.Text = e.StartTime;
                    lblEndTime = (Label)EventNotification3.FindControl("lblEndTime");
                    lblEndTime.Text = e.EndTime;
                    lblDeadline = (Label)EventNotification3.FindControl("lblDeadline");
                    lblDeadline.Text = e.EventDeadline.ToShortDateString();
                    lblDescription = (Label)EventNotification3.FindControl("lblDescription");
                    lblDescription.Text = e.Description;
                    lblNumber = (Label)EventNotification3.FindControl("lblNumber");
                    lblNumber.Text = p.NumberOfParticipant(lblEventName.Text).ToString();
                    break;
                case 4:
                    lblEventName = (Label)EventNotification4.FindControl("lblEventName");
                    lblEventName.Text = e.EventName;
                    lblVenue = (Label)EventNotification4.FindControl("lblVenue");
                    lblVenue.Text = e.EventVenue;
                    lblStartDate = (Label)EventNotification4.FindControl("lblStartDate");
                    lblStartDate.Text = e.StartDate.ToShortDateString();
                    lblEndDate = (Label)EventNotification4.FindControl("lblEndDate");
                    lblEndDate.Text = e.EndDate.ToShortDateString();
                    lblStartTime = (Label)EventNotification4.FindControl("lblStartTime");
                    lblStartTime.Text = e.StartTime;
                    lblEndTime = (Label)EventNotification4.FindControl("lblEndTime");
                    lblEndTime.Text = e.EndTime;
                    lblDeadline = (Label)EventNotification4.FindControl("lblDeadline");
                    lblDeadline.Text = e.EventDeadline.ToShortDateString();
                    lblDescription = (Label)EventNotification4.FindControl("lblDescription");
                    lblDescription.Text = e.Description;
                    lblNumber = (Label)EventNotification4.FindControl("lblNumber");
                    lblNumber.Text = p.NumberOfParticipant(lblEventName.Text).ToString();
                    break;
                case 5:
                    lblEventName = (Label)EventNotification5.FindControl("lblEventName");
                    lblEventName.Text = e.EventName;
                    lblVenue = (Label)EventNotification5.FindControl("lblVenue");
                    lblVenue.Text = e.EventVenue;
                    lblStartDate = (Label)EventNotification5.FindControl("lblStartDate");
                    lblStartDate.Text = e.StartDate.ToShortDateString();
                    lblEndDate = (Label)EventNotification5.FindControl("lblEndDate");
                    lblEndDate.Text = e.EndDate.ToShortDateString();
                    lblStartTime = (Label)EventNotification5.FindControl("lblStartTime");
                    lblStartTime.Text = e.StartTime;
                    lblEndTime = (Label)EventNotification5.FindControl("lblEndTime");
                    lblEndTime.Text = e.EndTime;
                    lblDeadline = (Label)EventNotification5.FindControl("lblDeadline");
                    lblDeadline.Text = e.EventDeadline.ToShortDateString();
                    lblDescription = (Label)EventNotification5.FindControl("lblDescription");
                    lblDescription.Text = e.Description;
                    lblNumber = (Label)EventNotification5.FindControl("lblNumber");
                    lblNumber.Text = p.NumberOfParticipant(lblEventName.Text).ToString();
                    break;
                case 6:
                    lblEventName = (Label)EventNotification6.FindControl("lblEventName");
                    lblEventName.Text = e.EventName;
                    lblVenue = (Label)EventNotification6.FindControl("lblVenue");
                    lblVenue.Text = e.EventVenue;
                    lblStartDate = (Label)EventNotification6.FindControl("lblStartDate");
                    lblStartDate.Text = e.StartDate.ToShortDateString();
                    lblEndDate = (Label)EventNotification6.FindControl("lblEndDate");
                    lblEndDate.Text = e.EndDate.ToShortDateString();
                    lblStartTime = (Label)EventNotification6.FindControl("lblStartTime");
                    lblStartTime.Text = e.StartTime;
                    lblEndTime = (Label)EventNotification6.FindControl("lblEndTime");
                    lblEndTime.Text = e.EndTime;
                    lblDeadline = (Label)EventNotification6.FindControl("lblDeadline");
                    lblDeadline.Text = e.EventDeadline.ToShortDateString();
                    lblDescription = (Label)EventNotification6.FindControl("lblDescription");
                    lblDescription.Text = e.Description;
                    lblNumber = (Label)EventNotification6.FindControl("lblNumber");
                    lblNumber.Text = p.NumberOfParticipant(lblEventName.Text).ToString();
                    break;
                case 7:
                    lblEventName = (Label)EventNotification7.FindControl("lblEventName");
                    lblEventName.Text = e.EventName;
                    lblVenue = (Label)EventNotification7.FindControl("lblVenue");
                    lblVenue.Text = e.EventVenue;
                    lblStartDate = (Label)EventNotification7.FindControl("lblStartDate");
                    lblStartDate.Text = e.StartDate.ToShortDateString();
                    lblEndDate = (Label)EventNotification7.FindControl("lblEndDate");
                    lblEndDate.Text = e.EndDate.ToShortDateString();
                    lblStartTime = (Label)EventNotification7.FindControl("lblStartTime");
                    lblStartTime.Text = e.StartTime;
                    lblEndTime = (Label)EventNotification7.FindControl("lblEndTime");
                    lblEndTime.Text = e.EndTime;
                    lblDeadline = (Label)EventNotification7.FindControl("lblDeadline");
                    lblDeadline.Text = e.EventDeadline.ToShortDateString();
                    lblDescription = (Label)EventNotification7.FindControl("lblDescription");
                    lblDescription.Text = e.Description;
                    lblNumber = (Label)EventNotification7.FindControl("lblNumber");
                    lblNumber.Text = p.NumberOfParticipant(lblEventName.Text).ToString();
                    break;
                case 8:
                    lblEventName = (Label)EventNotification8.FindControl("lblEventName");
                    lblEventName.Text = e.EventName;
                    lblVenue = (Label)EventNotification8.FindControl("lblVenue");
                    lblVenue.Text = e.EventVenue;
                    lblStartDate = (Label)EventNotification8.FindControl("lblStartDate");
                    lblStartDate.Text = e.StartDate.ToShortDateString();
                    lblEndDate = (Label)EventNotification8.FindControl("lblEndDate");
                    lblEndDate.Text = e.EndDate.ToShortDateString();
                    lblStartTime = (Label)EventNotification8.FindControl("lblStartTime");
                    lblStartTime.Text = e.StartTime;
                    lblEndTime = (Label)EventNotification8.FindControl("lblEndTime");
                    lblEndTime.Text = e.EndTime;
                    lblDeadline = (Label)EventNotification8.FindControl("lblDeadline");
                    lblDeadline.Text = e.EventDeadline.ToShortDateString();
                    lblDescription = (Label)EventNotification8.FindControl("lblDescription");
                    lblDescription.Text = e.Description;
                    lblNumber = (Label)EventNotification8.FindControl("lblNumber");
                    lblNumber.Text = p.NumberOfParticipant(lblEventName.Text).ToString();
                    break;
                case 9:
                    lblEventName = (Label)EventNotification9.FindControl("lblEventName");
                    lblEventName.Text = e.EventName;
                    lblVenue = (Label)EventNotification9.FindControl("lblVenue");
                    lblVenue.Text = e.EventVenue;
                    lblStartDate = (Label)EventNotification9.FindControl("lblStartDate");
                    lblStartDate.Text = e.StartDate.ToShortDateString();
                    lblEndDate = (Label)EventNotification9.FindControl("lblEndDate");
                    lblEndDate.Text = e.EndDate.ToShortDateString();
                    lblStartTime = (Label)EventNotification9.FindControl("lblStartTime");
                    lblStartTime.Text = e.StartTime;
                    lblEndTime = (Label)EventNotification9.FindControl("lblEndTime");
                    lblEndTime.Text = e.EndTime;
                    lblDeadline = (Label)EventNotification9.FindControl("lblDeadline");
                    lblDeadline.Text = e.EventDeadline.ToShortDateString();
                    lblDescription = (Label)EventNotification9.FindControl("lblDescription");
                    lblDescription.Text = e.Description;
                    lblNumber = (Label)EventNotification9.FindControl("lblNumber");
                    lblNumber.Text = p.NumberOfParticipant(lblEventName.Text).ToString();
                    break;
                case 10:
                    lblEventName = (Label)EventNotification10.FindControl("lblEventName");
                    lblEventName.Text = e.EventName;
                    lblVenue = (Label)EventNotification10.FindControl("lblVenue");
                    lblVenue.Text = e.EventVenue;
                    lblStartDate = (Label)EventNotification10.FindControl("lblStartDate");
                    lblStartDate.Text = e.StartDate.ToShortDateString();
                    lblEndDate = (Label)EventNotification10.FindControl("lblEndDate");
                    lblEndDate.Text = e.EndDate.ToShortDateString();
                    lblStartTime = (Label)EventNotification10.FindControl("lblStartTime");
                    lblStartTime.Text = e.StartTime;
                    lblEndTime = (Label)EventNotification10.FindControl("lblEndTime");
                    lblEndTime.Text = e.EndTime;
                    lblDeadline = (Label)EventNotification10.FindControl("lblDeadline");
                    lblDeadline.Text = e.EventDeadline.ToShortDateString();
                    lblDescription = (Label)EventNotification10.FindControl("lblDescription");
                    lblDescription.Text = e.Description;
                    lblNumber = (Label)EventNotification10.FindControl("lblNumber");
                    lblNumber.Text = p.NumberOfParticipant(lblEventName.Text).ToString();
                    break;
            }
        }

        public void Hide(int i)
        {
            switch (i)
            {
                case 1:
                    EventNotification1.Visible = false;
                    break;
                case 2:
                    EventNotification2.Visible = false;
                    break;
                case 3:
                    EventNotification3.Visible = false;
                    break;
                case 4:
                    EventNotification4.Visible = false;
                    break;
                case 5:
                    EventNotification5.Visible = false;
                    break;
                case 6:
                    EventNotification6.Visible = false;
                    break;
                case 7:
                    EventNotification7.Visible = false;
                    break;
                case 8:
                    EventNotification8.Visible = false;
                    break;
                case 9:
                    EventNotification9.Visible = false;
                    break;
                case 10:
                    EventNotification10.Visible = false;
                    break;
            }
        }
    }
}